import java.util.Stack;

public class RPN {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i < tokens.length;i++){
            String s = tokens[i];//"12"
            if(!isOperation(s)) {
                //数字
                Integer ret = Integer.valueOf(s);
                stack.push(ret);
            }else {
                Integer val2 = stack.pop();
                Integer val1 = stack.pop();
                switch(s) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperation(String val) {
        if (val.equals("+") || val.equals("-") || val.equals("*") || val.equals("/")) {
            return true;
        }
        return false;
    }
}
